<template>
    <div class="page">
        <gzg-search :config="searchConfig"
                    :showMore="true" 
                    :form="searchForm"
                    :label-width="120"
                    @search="search"></gzg-search>
        <div class="tool-wrap">     
            <div class="item">
                <div class="label">笔数:</div>
                <div class="val">{{ stat_data.count_num }}</div>
            </div> 
            <div class="item">
                <div class="label">分成总金额:</div>
                <div class="val">{{ stat_data.income_total }}</div>
            </div>  
            <div class="item">
                <div class="label">已提现金额:</div>
                <div class="val">{{ stat_data.withdraw_total }}</div>
            </div>  
            <div class="item">
                <div class="label">未清算金额:</div>
                <div class="val">{{ stat_data.wait_settle_total }}</div>
            </div>
            <div class="item">
                <div class="label">未提现金额:</div>
                <div class="val">{{ stat_data.surplus_total }}</div>
            </div>
            <el-button class="btn" type="primary" @click="exportExcel">导出</el-button>
        </div>
        <div class="grid-wrap">
            <el-table style="width:100%;"
                              height="100%"
                              :border="true"
                              :data="listData.data">
                <el-table-column label="序号" type="index" width="60px" fixed="left"></el-table-column>
                <el-table-column label="商户名称" prop="company_name" width="150px" fixed="left"></el-table-column>
                <el-table-column label="订单类型" prop="relation_type_desc" min-width="150px"></el-table-column>
                <el-table-column label="订单编号" prop="order_no" min-width="200px"></el-table-column>
                <el-table-column label="商品名称" prop="product_name" min-width="120px"></el-table-column>
                <el-table-column label="用户姓名" prop="buy_name" width="120px"></el-table-column>
                <el-table-column label="期数" prop="period_num" width="100px"></el-table-column>
                <el-table-column label="收款金额" prop="pay_total" width="120px"></el-table-column>
                <el-table-column label="应扣日期" prop="due_date" width="150px"></el-table-column>
                <el-table-column label="收款时间" prop="income_at" width="180px"></el-table-column>
                <el-table-column label="分成金额" prop="income_amount" width="120px"></el-table-column>
                <el-table-column label="分类" prop="biz_type_desc" width="120px"></el-table-column>
                <!-- <el-table-column label="分成角色" prop="agent_type_desc" width="120px"></el-table-column>
                <el-table-column label="分成人" prop="agent_real_name" width="120px"></el-table-column>
                <el-table-column label="分成人账号" prop="agent_mobile" width="120px"></el-table-column> -->
                <el-table-column label="是否提现" prop="withdraw_status_desc" width="100px"></el-table-column>
                <el-table-column label="提现单号" prop="withdraw_no" width="200px"></el-table-column>           
            </el-table>
        </div>
        <div class="page-wrap">
            <el-pagination
                v-model:current-page="params.page"
                v-model:page-size="params.page_size"
                :page-sizes="[100, 200, 300, 400]"
                layout="total, prev, pager, next"
                :total="listData.total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
            />
        </div>
      
    </div>
</template>
<script lang="ts" setup>
import { ref, reactive, watch, computed ,onMounted } from 'vue'

import { getCommissionLog,getIncomeCommissionExport } from 'services/commission'

import { assign } from 'lodash'

const searchForm = reactive({
    company_name:'',
    income_type:'',
    withdraw_status:'',
    start_date:'',
    end_date:'',
})

const searchConfig = reactive([{
    id:'company_name',
    label:'商户全称',
    placeholder:'请输入商户全称',
    type: 'input'
},{
    id:'income_type',
    label:'订单类型',
    placeholder:'请选择订单类型',
    type: 'select',
    enum:'income_type_list'
},{
    id:'start_date',
    label:'收款开始日期',
    placeholder:'请选择收款开始日期',
    type:'date'
},{
    id:'end_date',
    label:'收款结束日期',
    placeholder:'请选择收款结束日期',
    type:'date'
},{
    id:'withdraw_status',
    label:'是否提现',
    placeholder:'请选择是否提现',
    type: 'select',
    enum:'withdraw_status_list'
}])

const listData = reactive({
    total:0,
    data:<any>[]
})

const params = ref({
    company_name:'',
    page_size:  20,
    page:  1,
})

const stat_data = ref({
    count_num:'0',
    income_total:'0',
    surplus_total:'0',
    wait_settle_total:'0',
    withdraw_total:'0',

})

const search = (searchParams:any) => {

    assign(params.value, searchParams)

    params.value.page = 1

    getList()
}

const handleSizeChange = (val: number) => {
    params.value.page_size = val
    params.value.page = 1
    getList();
}

const handleCurrentChange = (val:number) => {
    params.value.page = val
    getList()
} 

const getList = () => {
    getCommissionLog(params.value).then(res => {
        stat_data.value = res.stat_data || {
            count_num:'0',
            income_total:'0',
            surplus_total:'0',
            wait_settle_total:'0',
            withdraw_total:'0',

        }

        listData.total = parseInt(res.total_rows)
        listData.data = res.items || []
    })
}

const exportExcel = () => {
    getIncomeCommissionExport(params.value).then(res => {
        const link = document.createElement('a');
        link.href = res.download_url;
        link.setAttribute('download', '分成明细.csv');
        link.click();
        link.remove()
    })
}

onMounted(() => {
    getList()
})
</script>